﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;

namespace IHISCETeim.BLL
{
    public class AgentConfigController:BaseController
    {
        private static List<AgentConfigInfo> _configs;
        private static List<String> changedKeys = new List<string>();

        private static List<AgentConfigInfo> Configs
        {
            get
            {
                if (_configs == null)
                {
                    InitConfigs();
                }
                return _configs;
            }
        }
        private static void InitConfigs()
        {
            _configs = CBO.FillCollection<AgentConfigInfo>(AgentConfigDAL.GetAll());
        }

        private static void Reload()
        {
            _configs = null;
        }

        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            return Configs.Where(c => c.ConfigKey == key).Select(c => c.ConfigValue).FirstOrDefault();
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private static void SetValue(string key, string value)
        {
            AgentConfigInfo config = Configs.Where(c => c.ConfigKey == key).FirstOrDefault();
            if (config != null)
            {
                if (config.ConfigValue != value)
                {
                    changedKeys.Add(key);
                    config.ConfigValue = value;
                }
            }
            else
            {
                AgentConfigInfo obj = new AgentConfigInfo();
                obj.ConfigKey = key;
                obj.ConfigValue = value;
                Configs.Add(obj);
            }
        }

        /// <summary>
        /// 保存所有更改的配置项
        /// </summary>
        public static bool SaveAll()
        {

            if (AgentConfigDAL.Update(Configs))
            {
                Reload();
                return true;
            }
            else
            {
                Reload();
                return false;
            }
            
        }


        /// <summary>
        /// 品情 服务经理用户ID
        /// </summary>
        private static int SvrManagerUserId
        {
            get
            {
                return Convert.ToInt32( GetValue("SvrManagerUserId"));
            }
            set
            {
                SetValue("SvrManagerUserId", value.ToString());
            }
        }
        /// <summary>
        /// 品情 配件发货提醒接收人
        /// </summary>
        public static string PartsManagerEmails
        {
            get
            {
                return GetValue("PartsManagerEmails");
            }
            set
            {
                SetValue("PartsManagerEmails", value);
            }
        }
        /// <summary>
        /// 管理供应商发货人员EMAIL
        /// </summary>
        public static string IHISCESvrSupperEmails
        {
            get
            {
                return GetValue("IHISCESvrSupperEmails");
            }
            set
            {
                SetValue("IHISCESvrSupperEmails", value);
            }
        }

        #region 抄送人员配置
        /// <summary>
        /// 抄送部门类
        /// </summary>
        public class QltyCCInfo{
            /// <summary>
            /// 抄送部门中文名称
            /// </summary>
            public string ccpartName{get;set;}
            /// <summary>
            /// 抄送部门配置Key
            /// </summary>
            public string ccpartKey{get;set;}
        }
        private static List<QltyCCInfo> _ccpartlist = null;
        public static List<QltyCCInfo> GetQltyccPartList()
        {
            if (_ccpartlist == null)
            {
                _ccpartlist = new List<QltyCCInfo>();
                _ccpartlist.Add(new QltyCCInfo() { ccpartKey = "QltyccZJLEmail", ccpartName = "总经理" });
                _ccpartlist.Add(new QltyCCInfo() { ccpartKey = "QltyccHWEmail", ccpartName = "服务内部" });
                _ccpartlist.Add(new QltyCCInfo() { ccpartKey = "QltyccBZEmail", ccpartName = "品质部" });
                _ccpartlist.Add(new QltyCCInfo() { ccpartKey = "QltyccSCBEmail", ccpartName = "生产本部" });
                _ccpartlist.Add(new QltyCCInfo() { ccpartKey = "QltyccSCEmail", ccpartName = "生产部" });
                _ccpartlist.Add(new QltyCCInfo() { ccpartKey = "QltyccJSEmail", ccpartName = "技术部" });
            }
            return _ccpartlist;
        }
        /// <summary>
        /// 总经理
        /// </summary>
        public static string QltyccZJLEmail
        {
            get
            {
                return GetValue("QltyccZJLEmail");
            }
            set
            {
                SetValue("QltyccZJLEmail", value);
            }
        }
        /// <summary>
        /// 服务内部
        /// </summary>
        public static string QltyccHWEmail
        {
            get
            {
                return GetValue("QltyccHWEmail");
            }
            set
            {
                SetValue("QltyccHWEmail", value);
            }
        }
        /// <summary>
        /// 品质部
        /// </summary>
        public static string QltyccBZEmail
        {
            get
            {
                return GetValue("QltyccBZEmail");
            }
            set
            {
                SetValue("QltyccBZEmail", value);
            }
        }
        /// <summary>
        /// 生产本部
        /// </summary>
        public static string QltyccSCBEmail
        {
            get
            {
                return GetValue("QltyccSCBEmail");
            }
            set
            {
                SetValue("QltyccSCBEmail", value);
            }
        }
        /// <summary>
        /// 生产部
        /// </summary>
        public static string QltyccSCEmail
        {
            get
            {
                return GetValue("QltyccSCEmail");
            }
            set
            {
                SetValue("QltyccSCEmail", value);
            }
        }
        /// <summary>
        /// 技术部
        /// </summary>
        public static string QltyccJSEmail
        {
            get
            {
                return GetValue("QltyccJSEmail");
            }
            set
            {
                SetValue("QltyccJSEmail", value);
            }
        }
        #endregion


        #region 索赔金额超出限制配置
        /// <summary>
        /// 报警金额
        /// </summary>
        public static Decimal QltyAlarmMoney
        {
            get
            {
                return Convert.ToDecimal( GetValue("QltyAlarmMoney"));
            }
            set
            {
                SetValue("QltyAlarmMoney", value.ToString());
            }
        }
        /// <summary>
        /// 确认后抄送邮箱
        /// </summary>
        public static string QltyAlarmEmail
        {
            get
            {
                return GetValue("QltyAlarmEmail");
            }
            set
            {
                SetValue("QltyAlarmEmail", value);
            }
        }
        #endregion
        #region 索赔权限设置相关
        public static int AddPowerConfig(AgentPowerConfigInfo obj)
        {
            DC.Remove(AgentPowerConfigKey);
            return AgentConfigDAL.AddPowerConfig(obj);
        }
        public static bool DeletePowerConfig(int ID)
        {
            DC.Remove(AgentPowerConfigKey);
            return AgentConfigDAL.DeletePowerConfig(ID);
        }
        public static  List<AgentPowerConfigInfo> GetAllPowerConfig()
        {
            List<AgentPowerConfigInfo> list = DC.Get<List<AgentPowerConfigInfo>>(AgentPowerConfigKey);
            if (list == null)
            { 
                list=CBO.FillCollection<AgentPowerConfigInfo>( AgentConfigDAL.GetAllPowerConfig());
                DC.Add(AgentPowerConfigKey,list);
            }

            return list;
        }

        public static AgentPowerConfigInfo GetById(int id)
        {
            List<AgentPowerConfigInfo> list = GetAllPowerConfig();
            AgentPowerConfigInfo r=  list.Find(e => e.ID == id);
            return r == null ? new AgentPowerConfigInfo(): r;

        }
        public static List<AgentPowerConfigInfo> GetByParId(int ParID)
        {
            List<AgentPowerConfigInfo> list = GetAllPowerConfig();
            return list.Where(e => e.ParId == ParID).ToList<AgentPowerConfigInfo>();
        }
        public static bool UpdatePowerConfig(AgentPowerConfigInfo obj)
        {
            DC.Remove(AgentPowerConfigKey);
            return AgentConfigDAL.UpdatePowerConfig(obj);
        }

        /// <summary>
        /// 根据用户ID,获取其上级服务领导
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public static int GetSvrManagerUserId(int UserId)
        {
            List<AgentPowerConfigInfo> list = GetAllPowerConfig();
            int parid = list.Find(e => e.UserId == UserId).ParId;

            return GetById(parid).UserId;
        }
        /// <summary>
        /// 获取服务人员权限信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public static AgentPowerConfigInfo GetByUserId(int UserId)
        {
            List<AgentPowerConfigInfo> list = GetAllPowerConfig();
            AgentPowerConfigInfo obj=list.Find(e => e.UserId == UserId);
            return obj == null ? new AgentPowerConfigInfo() { ShowClaimState="=="} : obj;
        }
        /// <summary>
        /// 根据服务人员获取最高上级人员
        /// </summary>
        /// <param name="SvrUserId"></param>
        /// <returns></returns>
        public static AgentPowerConfigInfo GetTopSvrMgr(int SvrUserId)
        {
            int userId = SvrUserId;
            AgentPowerConfigInfo power = null;
            while (true)
            {
                power = AgentConfigController.GetByUserId(userId);
                if (power.ParId == 0)
                    break;
                power = AgentConfigController.GetById(power.ParId);
                userId = power.UserId;                 
            }
            return power;
           // return OUserController.GetById(userId);
        }
        #endregion

        #region VIP客户相关
        /// <summary>
        /// VIP客户配件采购审核人员
        /// </summary>
        public static string VIPCustomerBuyPassEmails
        {
            get
            {
                return GetValue("VIPCustomerBuyPassEmails");
            }
            set
            {
                SetValue("VIPCustomerBuyPassEmails", value);
            }
        }
        /// <summary>
        /// 保外业务配件审核人员邮件
        /// </summary>
        public static string PWPartPassEmails
        {
            get
            {
                return GetValue("PWPartPassEmails");
            }
            set
            {
                SetValue("PWPartPassEmails", value);
            }
        }
        #endregion
    }
}
